import { dialog } from 'electron'
const path = require('path');
const fs = require('fs');
const { getFiles } = require('./getFiles')

const mainWindow = {};

export async function handleFileOpen(config) {
  const method = config.mothod ? config.mothod : 'openFile';
  const { canceled, filePaths } = await dialog.showOpenDialog({
    properties: [method]
  })
  if (canceled) {
    return
  } else {
    if (config.type) {
      const files = await getFiles(filePaths, config.type, config.mothod)
      if (files.length > 200) {
        const choice = dialog.showMessageBoxSync(mainWindow, {
          type: 'warning',
          buttons: ['确定'],
          title: '警告',
          message: '单次选的的图片超过上限!',
        })
        if (choice == 0) {
          return
        }
      } else {
        return files// 返回文件名给渲染进程
      }
    }
    return filePaths[0]// 返回文件名给渲染进程
  }
}